<?php
include('connect_db.php');

$err = null;

/*if logged in, redirect to homepage */
if (!checkLoggedin())
{
	header('Location: login.php');
}

if(ISSET($_GET['id']))
{
	$bill_id = $_GET['id'];
	$query = "SELECT * FROM bills
			  	WHERE id = " . $bill_id
              	." AND bills.user_id = " . $_SESSION['id'];
	$bill = mysql_fetch_array(mysql_query($query));

	if(!ISSET($bill['amount']))
	{
		header('Location: index.php');
	}
}
else
{
	header('Location: index.php');
}



?>
<?php include('templates/main/header.php'); ?>

    <div id="body">
		<div id="content" class="right_sidebar">
		<div id="addbill">
		<hr>
		<h1>Edit Bill</h1>
		<hr>
		<br />
		<br />
		<form action="editbill_2.php" method="post" accept-charset="utf-8">
		<h2>Household Members</h2>
		<ul>
			<?php
		        $query = "SELECT * FROM houses_users
		        JOIN users ON users.id = houses_users.user_id
		        WHERE houses_users.house_id = " . $bill['house_id'];
		        $results = mysql_query($query);//house users

		        while($row = mysql_fetch_array($results))
		        {
		        	if($row['user_id'] != $_SESSION['id'])//do not show user himself
		        	{
		        		echo '<li><label><input name="checked_fields[]" value="'.$row['user_id'].'" type="checkbox" ';

		        		$query2 = "SELECT * FROM bills_users
		        					JOIN users ON users.id = bills_users.user_id
		        					WHERE bill_id = " . $bill_id;
		        		$users = mysql_query($query2);//bill users 		
				        while($user = mysql_fetch_array($users))
				        {
				        	if($row['user_id'] == $user['user_id'])//do not show user himself
				        	{
								echo 'checked="checked"';
								break;
							}
						}
		        		echo '>' .$row['lastname'].' '.$row['firstname'].'</label></li>';
		        	}
		        }
			?>
		</ul>
		
		<label>
			Amount($)*:
			<input name="amount" value= <?php echo '"'.$bill['amount'].'"' ?> type="text">
		</label>

		<label>
			Reason:
			<textarea name="notes" cols="90" rows="7" class="markup_textbox markItUpEditor"><?php echo $bill['notes'] ?></textarea>
		</label>

		<input type="hidden" name="bill_id" value="<?php echo $bill['id']; ?>">

		<label>
			<div class="sameline">Category:</div>
			<select name="category_id">
			<option value="1" <?php echo $bill['category_id'] == 1 ? ' selected="selected"' : '';?> >Rent</option>
			<option value="2" <?php echo $bill['category_id'] == 2 ? ' selected="selected"' : '';?> >Electricity</option>
			<option value="3" <?php echo $bill['category_id'] == 3 ? ' selected="selected"' : '';?> >Water</option>
			<option value="4" <?php echo $bill['category_id'] == 4 ? ' selected="selected"' : '';?> >Gas</option>
			<option value="5" <?php echo $bill['category_id'] == 5 ? ' selected="selected"' : '';?> >Cable</option>
			<option value="6" <?php echo $bill['category_id'] == 6 ? ' selected="selected"' : '';?> >Food</option>
			<option value="7" <?php echo $bill['category_id'] == 7 ? ' selected="selected"' : '';?> >House supplies</option>
			<option value="8" <?php echo $bill['category_id'] == 8 ? ' selected="selected"' : '';?> >Entertainment</option>
			<option value="9" <?php echo $bill['category_id'] == 9 ? ' selected="selected"' : '';?> >Repair</option>
			<option value="10" <?php echo $bill['category_id'] == 10 ? ' selected="selected"' : '';?> >Transportation</option>
			</select>
			<option value="11" <?php echo $bill['category_id'] == 11 ? ' selected="selected"' : '';?> >Other</option>
			</select>
		</label>
				
		<input name="" value="Update" type="submit">
		<span class="error"><p><?php if ($err) echo $err; ?></p></span>
		</form>
		</div>
	</div>
	</div>

<?php include('templates/main/footer.php'); ?>